//
//  VisitorView.swift
//  Weibo
//
//  Created by 张婷 on 15/10/16.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate: NSObjectProtocol {
    
    func visitorViewDidRegister()
    
    func visitorViewDidLogin()
}

class VisitorView: UIView {
    
    weak var delegate: VisitorViewDelegate?
    
    // 监听方法
    @objc private func clickRegister() {
        delegate?.visitorViewDidRegister()
    }
    
    @objc private func clickLogin() {
        delegate?.visitorViewDidLogin()
    }
    
    // MARK: 设置视图信息
    func setupInfo(title: String, imageName: String?) {
        
        messageLabel.text = title
        
        guard let imgName = imageName else {
            startAnim()
            return
        }
       
        iconView.image = UIImage(named: imgName)
        
        // 小房子隐藏
        homeIconView.hidden = true
        // 遮罩层放到最后
        sendSubviewToBack(maskIconView)
    }
    
    /// 开启首页动画
    private func startAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MRAK: 构造函数
    // initWithFrame 是 UIView 指定的构造函数
    // 使用纯代码开发的口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    // initWithCoder 是使用 SB & XIB 开发加载的函数
    // SB 的入口
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    // MARK: - 懒加载控件
    // 使用 image: 构造函数创建的 imageView 默认就是 image 的大小
    /// 图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    /// 小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    /// 遮罩图像
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    // 消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回头看看有神马惊喜")

    // 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
  
    // 登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
  
}

// MARK: 设置界面
extension VisitorView {
    
    private func setupUI() {
        
        // 1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.自动布局
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 1> 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 3> 消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        // 6> 遮罩图片
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "regButton": registerButton]))
        
        // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        registerButton.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)

    }
}
